package com.ext.aic.apis.chat;

import com.ext.aic.AICApplication;
import com.ext.aic.beans.AgentReplySyncResult;
import com.ext.aic.beans.box.ReplyRequestLog;
import com.ext.aic.beans.params.AgentReplyParams;
import com.ext.aic.beans.response.AgentReplyResponse;
import com.ext.aic.beans.response.AgentReplySyncResponse;
import com.ext.aic.box.logs.WxReplyRequestLogsManager;
import com.ext.aic.constants.ApiConstants;
import com.ext.aic.utils.http.HttpUtils;
import com.eyinfo.android_pure_utils.logs.CrashUtils;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.eyinfo.android_pure_utils.utils.PathsUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AgentReplySyncRequest {

    protected void onAgentReplySyncSuccess(AgentReplySyncResult result, AgentReplyResponse replyResponse) {

    }

    public void request(AgentReplyParams params) {
        WxReplyRequestLogsManager.getInstance().start();

        AgentReplySyncResult result = new AgentReplySyncResult();
        result.setUsername(params.getUsername());
        result.setSessionId(params.getSessionId());

        String url = PathsUtils.combine(ApiConstants.baseUrlForAI, "chat");
        String json = JsonUtils.toJson(params);
        RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
        Request request = new Request.Builder().url(url).post(requestBody).addHeader("Content-Type", "application/json").build();
        OkHttpClient client = HttpUtils.getInstance().getClient(AICApplication.getInstance().getApplicationContext());
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    AgentReplySyncResponse syncResponse = JsonUtils.parseT(responseBody, AgentReplySyncResponse.class);
                    if (syncResponse.getCode() == 200) {
                        AgentReplyResponse replyResponse = syncResponse.getData();
                        result.setContent(replyResponse.getContent());
                        onAgentReplySyncSuccess(result, replyResponse);
                    } else {
                        printLog(params, null, syncResponse.getMessage(), 3);
                        request(params);
                    }
                } else {
                    printLog(params, null, response.message(), 4);
                    request(params);
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                String crashInfo = CrashUtils.getCrashInfo(e);
                printLog(params, null, crashInfo, 5);
                request(params);
            }
        });
    }

    public void printLog(AgentReplyParams params, AgentReplyResponse response, String crash, int status) {
//        ReplyRequestLog requestLog = new ReplyRequestLog();
//        requestLog.setId(WxReplyRequestLogsManager.getInstance().getLogId());
//        requestLog.setRequest(JsonUtils.toJson(params));
//        if (response != null) {
//            requestLog.setResponse(JsonUtils.toJson(response));
//        }
//        requestLog.setReasons(crash);
//        requestLog.setStatus(status);
//        WxReplyRequestLogsManager.getInstance().recordLog(requestLog);
    }
}
